/**
 * 
 */
package cu.ds.logicalsync.fytasrealm.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import org.jdesktop.swingx.JXTreeTable;

import cu.ds.logicalsync.fytasrealm.FytasRealm;
import cu.ds.logicalsync.fytasrealm.ui.panel.PeerListPanel;
import cu.ds.logicalsync.fytasrealm.ui.panel.PrototypeCommandPanel;
import cu.ds.logicalsync.fytasrealm.util.Resources;

/**
 * Main User Interface component 
 * 
 * @author r.vashira
 */
public class PrototypeUI extends JFrame implements WindowListener {
	private FytasRealm m_application;
	private JXTreeTable m_peersTree;
	//private 
	
	/**
	 * Constructor
	 * @param p_application
	 */
	public PrototypeUI(FytasRealm p_application) {
		m_application = p_application;
		initialize();
		buildUI();
	}
	
	private void buildUI() {
		// Destroy on closing
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	/**
	 * Create Peers Tree
	 * TODO: Extract to separated class
	 * @deprecated
	 */
	private JXTreeTable buildPeersTree() {
		return null;
	}

	/**
	 * Open Login dialog
	 * TODO: Create custom dialog for login
	 * @return
	 */
	public Object openLoginDialog() {
		return JOptionPane.showInputDialog(null, "Please enter your name", "Login",
				  JOptionPane.QUESTION_MESSAGE, null, null, "Name");
	}
	
	public void setClientName(String clientName) {
		this.setTitle(Resources.getString("title.prototype") + " : " + clientName);
	}
	
	
	/*---------- GENERATED CODE -------------*/
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JSplitPane splitter = null;
	private PeerListPanel peersPanel = null;
	private PrototypeCommandPanel contentPanel = null;
	private JPanel footerPanel = null;

	/**
	 * This is the default constructor
	 */
	public PrototypeUI() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 300);
		this.setContentPane(getJContentPane());
		this.setTitle(Resources.getString("title.prototype"));
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagLayout gb = new GridBagLayout();
	        GridBagConstraints gbc = new GridBagConstraints();
	        
	        gbc.gridx = 0;
	        gbc.gridy = 0;
	        gbc.gridwidth = 1;
	        gbc.gridheight = 1;
	        gbc.insets = new Insets(3, 3, 3, 3);
	        gbc.anchor = GridBagConstraints.NORTHWEST;
	        gbc.weightx = 1.0;
	        gbc.weighty = 1.0;
	        gbc.fill = GridBagConstraints.BOTH;
			
			jContentPane = new JPanel();
			jContentPane.setLayout(gb);
			jContentPane.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
			jContentPane.add(getSplitter(), gbc);
			
			gbc.gridx = 0;
	        gbc.gridy++;
	        gbc.anchor = GridBagConstraints.LINE_START;
	        gbc.weightx = 0.0;
	        gbc.weighty = 0.0;
	        gbc.fill = GridBagConstraints.HORIZONTAL;
	        
	        JPanel fp = getFooterPanel();
	        gb.setConstraints(fp, gbc);
			
			jContentPane.add(fp);
		}
		return jContentPane;
	}

	/**
	 * This method initializes splitter	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getSplitter() {
		if (splitter == null) {
			splitter = new JSplitPane();
			splitter.setResizeWeight(0.15D);
			splitter.setDividerSize(10);
			splitter.setLeftComponent(getPeersPanel());
			splitter.setRightComponent(getContentPanel());
			splitter.setOneTouchExpandable(true);
		}
		return splitter;
	}

	/**
	 * This method initializes peersPanel	
	 * Public for accessible from FytaRealm
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public PeerListPanel getPeersPanel() {
		if (peersPanel == null) {
			peersPanel = new PeerListPanel();
		}
		return peersPanel;
	}

	private PrototypeCommandPanel getContentPanel() {
		if(contentPanel == null) {
			contentPanel = new PrototypeCommandPanel();
		}
		return contentPanel;
	}
	
	private JPanel getFooterPanel() {
		if(footerPanel == null) {
			footerPanel = new JPanel();
			JPanel infoPanel = new JPanel();
			
			footerPanel.setLayout(new BoxLayout(footerPanel, BoxLayout.X_AXIS));
			footerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			infoPanel.add(Box.createHorizontalGlue());
			
			JLabel l = new JLabel("<html><body>Fyta's Relam - from LogicalSync Group</body></html>", JLabel.TRAILING);
			l.setFont(l.getFont().deriveFont(8));
			infoPanel.add(l);
			
			infoPanel.setMinimumSize(new Dimension(280, 20));
	        //infoPanel.setBorder(new EmptyBorder(-5, 0, -5, 0));
	        footerPanel.add(infoPanel);
		}
		return footerPanel;
	}
	
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent e) {
		m_application.leaveGame();
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

}
